# RUN: llc -mtriple=aarch64-linux-gnu -mattr=+sve -run-pass=prologepilog -simplify-mir -verify-machineinstrs %s -o - | FileCheck %s
# RUN: llc -mtriple=aarch64-linux-gnu -mattr=+sve -start-before=prologepilog -simplify-mir -verify-machineinstrs %s -o - | FileCheck %s --check-prefix=CHECK-OFFSET

--- |
  define void @testcase_valid_offset() nounwind { entry: unreachable }
  define void @testcase_offset_out_of_range() nounwind { entry: unreachable }
...
---
name:            testcase_valid_offset
tracksRegLiveness: true
stack:
  - { id: 0, name: '', type: default, offset: 0, size: 512, alignment: 16, stack-id: scalable-vector }
body:             |
  bb.0:
    liveins: $p0

    ; CHECK-LABEL: name: testcase_valid_offset
    ; CHECK: liveins: $p0
    ; CHECK-NEXT: {{  $}}
    ; CHECK-NEXT: early-clobber $sp = frame-setup STRXpre killed $fp, $sp, -16 :: (store (s64) into %stack.1)
    ; CHECK-NEXT: $sp = frame-setup ADDVL_XXI $sp, -32
    ; CHECK-NEXT: renamable $z0_z1 = LD2B_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1 = LD2B_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1 = LD2H_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1 = LD2H_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1 = LD2W_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1 = LD2W_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1 = LD2D_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1 = LD2D_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3B_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3B_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3H_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3H_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3W_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3W_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3D_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3D_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, $sp, -8
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, $sp, 7
    ; CHECK-NEXT: $sp = frame-destroy ADDVL_XXI $sp, 31
    ; CHECK-NEXT: $sp = frame-destroy ADDVL_XXI $sp, 1
    ; CHECK-NEXT: early-clobber $sp, $fp = frame-destroy LDRXpost $sp, 16 :: (load (s64) from %stack.1)
    ; CHECK-NEXT: RET_ReallyLR implicit $z0, implicit $z1, implicit $z2, implicit $z3

    ; CHECK-OFFSET-LABEL: testcase_valid_offset:
    ; CHECK-OFFSET: str x29, [sp, #-16]!
    ; CHECK-OFFSET-NEXT: addvl sp, sp, #-32
    ; CHECK-OFFSET-NEXT: ld2b { z0.b, z1.b }, p0/z, [sp, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: ld2b { z0.b, z1.b }, p0/z, [sp, #14, mul vl]
    ; CHECK-OFFSET-NEXT: ld2h { z0.h, z1.h }, p0/z, [sp, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: ld2h { z0.h, z1.h }, p0/z, [sp, #14, mul vl]
    ; CHECK-OFFSET-NEXT: ld2w { z0.s, z1.s }, p0/z, [sp, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: ld2w { z0.s, z1.s }, p0/z, [sp, #14, mul vl]
    ; CHECK-OFFSET-NEXT: ld2d { z0.d, z1.d }, p0/z, [sp, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: ld2d { z0.d, z1.d }, p0/z, [sp, #14, mul vl]
    ; CHECK-OFFSET-NEXT: ld3b { z0.b - z2.b }, p0/z, [sp, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: ld3b { z0.b - z2.b }, p0/z, [sp, #21, mul vl]
    ; CHECK-OFFSET-NEXT: ld3h { z0.h - z2.h }, p0/z, [sp, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: ld3h { z0.h - z2.h }, p0/z, [sp, #21, mul vl]
    ; CHECK-OFFSET-NEXT: ld3w { z0.s - z2.s }, p0/z, [sp, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: ld3w { z0.s - z2.s }, p0/z, [sp, #21, mul vl]
    ; CHECK-OFFSET-NEXT: ld3d { z0.d - z2.d }, p0/z, [sp, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: ld3d { z0.d - z2.d }, p0/z, [sp, #21, mul vl]
    ; CHECK-OFFSET-NEXT: ld4b { z0.b - z3.b }, p0/z, [sp, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: ld4b { z0.b - z3.b }, p0/z, [sp, #28, mul vl]
    ; CHECK-OFFSET-NEXT: ld4h { z0.h - z3.h }, p0/z, [sp, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: ld4h { z0.h - z3.h }, p0/z, [sp, #28, mul vl]
    ; CHECK-OFFSET-NEXT: ld4w { z0.s - z3.s }, p0/z, [sp, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: ld4w { z0.s - z3.s }, p0/z, [sp, #28, mul vl]
    ; CHECK-OFFSET-NEXT: ld4d { z0.d - z3.d }, p0/z, [sp, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: ld4d { z0.d - z3.d }, p0/z, [sp, #28, mul vl]
    ; CHECK-OFFSET-NEXT: addvl sp, sp, #31
    ; CHECK-OFFSET-NEXT: addvl sp, sp, #1
    ; CHECK-OFFSET-NEXT: ldr x29, [sp], #16
    ; CHECK-OFFSET-NEXT: ret

    renamable $z0_z1 = LD2B_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1 = LD2B_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1 = LD2H_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1 = LD2H_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1 = LD2W_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1 = LD2W_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1 = LD2D_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1 = LD2D_IMM renamable $p0, %stack.0, 7

    renamable $z0_z1_z2 = LD3B_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2 = LD3B_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1_z2 = LD3H_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2 = LD3H_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1_z2 = LD3W_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2 = LD3W_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1_z2 = LD3D_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2 = LD3D_IMM renamable $p0, %stack.0, 7

    renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, %stack.0, 7
    renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, %stack.0, -8
    renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, %stack.0, 7
    RET_ReallyLR implicit $z0, implicit $z1, implicit $z2, implicit $z3
...
---
name:            testcase_offset_out_of_range
tracksRegLiveness: true
stack:
  - { id: 0, name: '', type: default, offset: 0, size: 512, alignment: 16, stack-id: scalable-vector }
body:             |
  bb.0:
    liveins: $p0

    ; CHECK-LABEL: name: testcase_offset_out_of_range
    ; CHECK: liveins: $p0
    ; CHECK-NEXT: {{  $}}
    ; CHECK-NEXT: early-clobber $sp = frame-setup STRXpre killed $fp, $sp, -16 :: (store (s64) into %stack.1)
    ; CHECK-NEXT: $sp = frame-setup ADDVL_XXI $sp, -32
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -2
    ; CHECK-NEXT: renamable $z0_z1 = LD2B_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 2
    ; CHECK-NEXT: renamable $z0_z1 = LD2B_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -2
    ; CHECK-NEXT: renamable $z0_z1 = LD2H_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 2
    ; CHECK-NEXT: renamable $z0_z1 = LD2H_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -2
    ; CHECK-NEXT: renamable $z0_z1 = LD2W_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 2
    ; CHECK-NEXT: renamable $z0_z1 = LD2W_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -2
    ; CHECK-NEXT: renamable $z0_z1 = LD2D_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 2
    ; CHECK-NEXT: renamable $z0_z1 = LD2D_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3B_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3B_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3H_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3H_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3W_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3W_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3D_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 3
    ; CHECK-NEXT: renamable $z0_z1_z2 = LD3D_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, -4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, killed $x8, -8
    ; CHECK-NEXT: $x8 = ADDVL_XXI $sp, 4
    ; CHECK-NEXT: renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, killed $x8, 7
    ; CHECK-NEXT: $sp = frame-destroy ADDVL_XXI $sp, 31
    ; CHECK-NEXT: $sp = frame-destroy ADDVL_XXI $sp, 1
    ; CHECK-NEXT: early-clobber $sp, $fp = frame-destroy LDRXpost $sp, 16 :: (load (s64) from %stack.1)
    ; CHECK-NEXT: RET_ReallyLR implicit $z0, implicit $z1, implicit $z2, implicit $z3

    ; CHECK-OFFSET-LABEL: testcase_offset_out_of_range:
    ; CHECK-OFFSET: str x29, [sp, #-16]!
    ; CHECK-OFFSET-NEXT: addvl sp, sp, #-32
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-2
    ; CHECK-OFFSET-NEXT: ld2b { z0.b, z1.b }, p0/z, [x8, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #2
    ; CHECK-OFFSET-NEXT: ld2b { z0.b, z1.b }, p0/z, [x8, #14, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-2
    ; CHECK-OFFSET-NEXT: ld2h { z0.h, z1.h }, p0/z, [x8, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #2
    ; CHECK-OFFSET-NEXT: ld2h { z0.h, z1.h }, p0/z, [x8, #14, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-2
    ; CHECK-OFFSET-NEXT: ld2w { z0.s, z1.s }, p0/z, [x8, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #2
    ; CHECK-OFFSET-NEXT: ld2w { z0.s, z1.s }, p0/z, [x8, #14, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-2
    ; CHECK-OFFSET-NEXT: ld2d { z0.d, z1.d }, p0/z, [x8, #-16, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #2
    ; CHECK-OFFSET-NEXT: ld2d { z0.d, z1.d }, p0/z, [x8, #14, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-3
    ; CHECK-OFFSET-NEXT: ld3b { z0.b - z2.b }, p0/z, [x8, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #3
    ; CHECK-OFFSET-NEXT: ld3b { z0.b - z2.b }, p0/z, [x8, #21, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-3
    ; CHECK-OFFSET-NEXT: ld3h { z0.h - z2.h }, p0/z, [x8, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #3
    ; CHECK-OFFSET-NEXT: ld3h { z0.h - z2.h }, p0/z, [x8, #21, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-3
    ; CHECK-OFFSET-NEXT: ld3w { z0.s - z2.s }, p0/z, [x8, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #3
    ; CHECK-OFFSET-NEXT: ld3w { z0.s - z2.s }, p0/z, [x8, #21, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-3
    ; CHECK-OFFSET-NEXT: ld3d { z0.d - z2.d }, p0/z, [x8, #-24, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #3
    ; CHECK-OFFSET-NEXT: ld3d { z0.d - z2.d }, p0/z, [x8, #21, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-4
    ; CHECK-OFFSET-NEXT: ld4b { z0.b - z3.b }, p0/z, [x8, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #4
    ; CHECK-OFFSET-NEXT: ld4b { z0.b - z3.b }, p0/z, [x8, #28, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-4
    ; CHECK-OFFSET-NEXT: ld4h { z0.h - z3.h }, p0/z, [x8, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #4
    ; CHECK-OFFSET-NEXT: ld4h { z0.h - z3.h }, p0/z, [x8, #28, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-4
    ; CHECK-OFFSET-NEXT: ld4w { z0.s - z3.s }, p0/z, [x8, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #4
    ; CHECK-OFFSET-NEXT: ld4w { z0.s - z3.s }, p0/z, [x8, #28, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #-4
    ; CHECK-OFFSET-NEXT: ld4d { z0.d - z3.d }, p0/z, [x8, #-32, mul vl]
    ; CHECK-OFFSET-NEXT: addvl x8, sp, #4
    ; CHECK-OFFSET-NEXT: ld4d { z0.d - z3.d }, p0/z, [x8, #28, mul vl]
    ; CHECK-OFFSET-NEXT: addvl sp, sp, #31
    ; CHECK-OFFSET-NEXT: addvl sp, sp, #1
    ; CHECK-OFFSET-NEXT: ldr x29, [sp], #16
    ; CHECK-OFFSET-NEXT: ret

    renamable $z0_z1 = LD2B_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1 = LD2B_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1 = LD2H_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1 = LD2H_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1 = LD2W_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1 = LD2W_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1 = LD2D_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1 = LD2D_IMM renamable $p0, %stack.0, 8

    renamable $z0_z1_z2 = LD3B_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2 = LD3B_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1_z2 = LD3H_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2 = LD3H_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1_z2 = LD3W_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2 = LD3W_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1_z2 = LD3D_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2 = LD3D_IMM renamable $p0, %stack.0, 8

    renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2_z3 = LD4B_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2_z3 = LD4H_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2_z3 = LD4W_IMM renamable $p0, %stack.0, 8
    renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, %stack.0, -9
    renamable $z0_z1_z2_z3 = LD4D_IMM renamable $p0, %stack.0, 8
    RET_ReallyLR implicit $z0, implicit $z1, implicit $z2, implicit $z3
...
